﻿using System;
using System.Text;

namespace LeRenard.Core.Cryptography.Base64
{
    /// <summary>
    /// Base64 decryptor.
    /// </summary>
    public sealed class Base64Decryptor : CryptographyBase, IDecryptor
    {
        /// <summary>
        /// Creates a new <see cref="Base64Decryptor"/>.
        /// </summary>
        /// <param name="encoding">The encoding to be used.</param>
        public Base64Decryptor(Encoding encoding)
            : base(encoding)
        {
        }

        /// <summary>
        /// Decrypts the specified input.
        /// </summary>
        /// <param name="input">The input to be decrypted.</param>
        public string Decrypt(string input)
        {
            var decryptedAsBytes = Convert.FromBase64String(input);
            var decryptedAsString = this.Encoding.GetString(decryptedAsBytes, 0, decryptedAsBytes.Length);

            return decryptedAsString;
        }

        /// <summary>
        /// Decrypts the specified input.
        /// </summary>
        /// <param name="input">The input to be decrypted.</param>
        /// <returns>The base64 string input to it's decrypted form.</returns>
        public string Decrypt(byte[] input)
        {
            ThrowIf.Null(() => input);

            var decryptedAsString = this.Encoding.GetString(input, 0, input.Length);

            return decryptedAsString;
        }
    }
}
